<template>
  <div class="bg">
    <!-- 时间搜索框 -->
    <!-- <searchTime
      @searchvalue="userSearch"
      :options="options"
      :selectBoolen="false"
      :selectTime="true"
    />-->

    <!-- 表格展示 -->
    <el-card class="card">
      <div slot="header">
        <el-row type="flex" justify="end">
          <el-col :span="2">
            <el-button type="success" size="small" @click="newrole">新建角色</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格展示 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="index" label="序号" width="100" align="center"></el-table-column>
        <el-table-column prop="roleName" label="角色名称" align="center"></el-table-column>
        <el-table-column prop="roleCreateTime" label="创建时间" align="center"></el-table-column>
        <el-table-column prop="roleStatus" label="状态" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.roleStatus==1?'正常':'禁用'}}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="lookrole(scope.row)">查看/编辑</el-button>
            <el-button type="danger" size="small" @click="roleDelet(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="total,  prev, pager, next, jumper"
        :total="total"
        :current-page="current"
        style="margin-top:24px"
        @current-change="currentpange"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: "",
      value1: "",
      downloadLoading: false,
      tableData: [],
      total: 0,
      current: 1
    };
  },
  mounted() {
    this.rolelist();
  },
  methods: {
    // 获取角色列表数据
    rolelist() {
      let params = {
        size: 10,
        current: this.current
      };
      this.$http.article.roleList(params).then(res => {
        console.log("获取角色列表", res);
        this.tableData = res.result.records;
        this.total = res.result.total;
        //显示每页展示的数量
        res.result.records.forEach((item, index) => {
          //res.data.data.list 表格数据     forEach为每一项添加数据
          item.index = index + 1 + (this.current - 1) * 10;
          // this.currentPage当前页  this.pageParams.size 每页显示多少    :page-sizes="[10,20,30,50]" 分页大小

          return item;
        });
      });
    },
    // 分页操作
    currentpange(val) {
      this.current = val;
      this.rolelist();
    },

    // 新增角色列表
    newrole() {
      this.$router.push("/home/newrole");
    },

    // 查看修改编辑
    lookrole(val) {
      console.log(val);
      this.$router.push({
        path: "/home/newrole",
        query: { roleId: val.roleId }
      });
    },

    // 删除角色列表
    roleDelet(val) {
      this.$confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.article.roledelete({ roleId: val.roleId }).then(res => {
            if (res.code == 200) this.$message.success("删除成功");
            /*--------删除成功之后获取上一页的数据,避免是否为空--------*/

            //此时已经实现删除操作，所以total的值需要减size，Math.ceil是向上取整，保证始终大于等于0
            const totalPage = Math.ceil((this.total - 1) / 10);

            console.log("totalPage", totalPage);

            //将当前页码与删除数据之后的总页数进行比较，避免当前页码不存在
            const pagenum = this.current > totalPage ? totalPage : this.current;

            console.log("获取总条数", pagenum);

            //避免pagenum变为0
            this.current = pagenum < 1 ? 1 : pagenum;

            this.rolelist();
            console.log("删除角色列表", res);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.merchantData {
  display: flex;
  align-items: center;
  padding-top: 14px;
  .addmerchant {
    color: #409eff;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 400;
  }
  .merchantNum {
    color: #f23a3a;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 800;
  }
}

.clearfix {
  padding-bottom: 20px;
}
.bg {
  width: 100%;
  height: 100%;
  background-color: #f4f7fd;
  overflow: hidden;
}

.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>